/**
 * 
 */
package com.android.contacts.model;

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.android.utility.Utils;

/**
 * @author amilad
 * 
 */
public class JExcelController {

	private static final String TAG = JExcelController.class.getSimpleName();
	private static WritableWorkbook mWorkbook = null;
	private WritableSheet mSheet = null;
	private File mFile = null;

	/**
	 * 
	 */
	public JExcelController(String xlsName) {
		try {
			setFilePath(xlsName);
			mWorkbook = Workbook.createWorkbook(mFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void createWorkSheet(String sheetName) {
		mSheet = mWorkbook.createSheet(sheetName, 0);
	}

	private void setFilePath(String fileName) {
		File path = new File(Utils.getMyExternalBackup());
		if (!path.exists()) {
			path.mkdir();
		}
		mFile = new File(path.getAbsolutePath(), fileName);
	}

	public File getWorkBookFile() {
		return mFile;
	}

	public void createCellNames() {
		try {
			for (int i = 0; i < mColumnNames.length; i++) {
				Label label = new Label(i, 0, mColumnNames[i]);
				mSheet.addCell(label);
				if (mColumnNames[i].equalsIgnoreCase("Address")) {
					Label addLabel = null;
					int length = mAddressColumns.length;
					mSheet.mergeCells(i, 0, length, 0);
					for (int j = 0; j <= length - 1; j++) {
						addLabel = new Label(i + j, i, mAddressColumns[j]);
						mSheet.addCell(addLabel);
					}

				}

				if (mColumnNames[i].equalsIgnoreCase("Phone")) {
					Label addLabel = null;
					int length = mPhoneColumns.length;
					for (int j = 0; j <= length - 1; j++) {
						addLabel = new Label(mAddressColumns.length + i + j, i,
								mPhoneColumns[j]);
						mSheet.addCell(addLabel);
					}
				}
				if (mColumnNames[i].equalsIgnoreCase("E-mail")) {
					Label addLabel = null;
					int length = mEmailColumns.length;
					for (int j = 0; j <= length - 1; j++) {
						addLabel = new Label(mAddressColumns.length
								+ mPhoneColumns.length + i + j, i,
								mEmailColumns[j]);
						mSheet.addCell(addLabel);
					}
				}
				if (mColumnNames[i].equalsIgnoreCase("Organization")) {
					Label addLabel = null;
					int length = mOrganizationColumns.length;
					for (int j = 0; j <= length - 1; j++) {
						addLabel = new Label(mAddressColumns.length
								+ mPhoneColumns.length + mEmailColumns.length
								+ i + j, i, mOrganizationColumns[j]);
						mSheet.addCell(addLabel);
					}
				}
			}
			mWorkbook.write();
			mWorkbook.close();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String[] mColumnNames = { "ID", "DISPLAY NAME", "Address", "Phone",
			"E-mail", "Notes", "Organization", "IM" };

	private String[] mAddressColumns = { "Street", "City", "State",
			"Postal Code", "Country", "Post Box", "Type" };

	private String[] mPhoneColumns = { "Type", "Number" };
	private String[] mEmailColumns = { "Type", "Id" };
	private String[] mOrganizationColumns = { "Name", "Title" };
}
